package com.carSys.Service;

import com.carSys.DTO.RepairPersonDTO;
import com.carSys.Entity.RepairPerson;
import com.carSys.Entity.User;
import com.carSys.Enums.TaskType;
import com.carSys.Enums.UserRole;
import com.carSys.Mapper.RepairPersonMapper;
import com.carSys.Mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RepairPersonService {
    @Autowired
    private RepairPersonMapper repairPersonMapper;

    @Autowired
    private UserMapper userMapper;

    // 查询所有维修人员的全部信息
    public List<RepairPersonDTO> getAllRepairPersons(){
        List<RepairPersonDTO> ret = new ArrayList<>();
        List<User> repairPersonList = userMapper.selectAllRepairPersons();
        for (User user : repairPersonList) {
            RepairPerson repairPerson = repairPersonMapper.selectRepairPersonByUserId(user.getUser_id());
            RepairPersonDTO repairPersonDTO = new RepairPersonDTO();
            repairPersonDTO.setUser_id(user.getUser_id());
            repairPersonDTO.setUsername(user.getUsername());
            repairPersonDTO.setPassword(user.getPassword());
            repairPersonDTO.setRepair_person_id(repairPerson.getRepair_person_id());
            repairPersonDTO.setSpecialty(repairPerson.getSpecialty());
            repairPersonDTO.setHourly_rate(repairPerson.getHourly_rate());
            ret.add(repairPersonDTO);
        }
        return ret;
    };

    // 创建维修人员
    @Transactional(rollbackFor = Exception.class)
    public boolean createRepairPerson(RepairPersonDTO repairPerson) {
        // 将DTO转换为User和RepairPerson对象
        User user = new User();
        user.setUsername(repairPerson.getUsername());
        user.setPassword(repairPerson.getPassword());
        user.setUserRole(UserRole.REPAIR_PERSON); // 设置用户角色为维修人员
        RepairPerson rp = new RepairPerson();
        rp.setSpecialty(repairPerson.getSpecialty());
        rp.setHourly_rate(repairPerson.getHourly_rate());
        // 插入User
        userMapper.addNewUser(user);
        User newUser = userMapper.selectUserByUserName(user.getUsername());
        if (newUser == null) {
            return false; // 用户插入失败
        }
        long userId = newUser.getUser_id();
        // 设置RepairPerson的user_id
        rp.setUser_id(userId);
        // 插入RepairPerson
        int repairPersonId = repairPersonMapper.insertRepairPerson(rp);
        if (repairPersonId <= 0) {
            userMapper.deleteUser(userId); // 回滚用户插入
            return false; // 维修人员插入失败
        }
        // 设置RepairPerson的ID
        rp.setRepair_person_id(repairPersonId);
        return true; // 成功创建维修人员
    }

    // 查询维修人员详情
    public RepairPerson getRepairPersonById(long repair_person_id) {
        return repairPersonMapper.selectRepairPersonById(repair_person_id);
    }

    // 根据用户ID查询维修人员
    public RepairPerson getRepairPersonByUserId(long user_id) {
        return repairPersonMapper.selectRepairPersonByUserId(user_id);
    }

    // 更新维修人员信息
    public boolean updateRepairPerson(RepairPerson repairPerson) {
        return repairPersonMapper.updateRepairPerson(repairPerson) > 0;
    }

    // 删除维修人员
    public boolean deleteRepairPerson(long userId, long repair_person_id) {
        int res1 = userMapper.deleteUser(userId);
        System.out.println(res1);
        return res1 > 0;
    }

    public List<RepairPerson> getRepairPersonBySpecialty(TaskType specialty) {
        return repairPersonMapper.selectRepairPersonBySpecialty(specialty);
    }

    // 更新全信息
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAllInfo(RepairPersonDTO repairPerson) {
        // 将dto装回user和repairPerson
        User user = new User();
        user.setUser_id(repairPerson.getUser_id());
        user.setUsername(repairPerson.getUsername());
        user.setPassword(repairPerson.getPassword());
        RepairPerson rp = new RepairPerson();
        rp.setUser_id(repairPerson.getUser_id());
        rp.setRepair_person_id(repairPerson.getRepair_person_id());
        rp.setSpecialty(repairPerson.getSpecialty());
        rp.setHourly_rate(repairPerson.getHourly_rate());
        // 分别进行更新
        boolean userUpdated = userMapper.updateUser(user) > 0;
        boolean rpUpdated = repairPersonMapper.updateRepairPerson(rp) > 0;
        return userUpdated && rpUpdated;
    }

    public RepairPersonDTO getRepairPersonDTOByUserId(long userId) {
        return repairPersonMapper.getRepairPersonDTOByUserId(userId);
    }
}